package com.itheima.reggie.config.common;

import com.itheima.reggie.web.exception.BusinessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 异常处理
 *
 * @Name GlobalExceptionHandler
 * @Author BaiXin
 * @Date 2022-10-30 10:42
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R doSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException ex){
        ex.printStackTrace();
        return R.error("员工姓名已经存在,请重新输入");
    }
    @ExceptionHandler(CustomerException.class)
    public R doCustomerException(CustomerException ex){
        ex.printStackTrace();
        return R.error(ex.getMessage());
    }

    /**
     * 处理业务夜异常
     * @param ex
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public R doBusinessException(BusinessException ex){
        ex.printStackTrace();
        return R.error(ex.getMessage());
    }
}
